package com.twodstar.photobattle.slidemenu;

import java.util.Arrays;
import java.util.List;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;
import com.twodstar.photobattle.Fragment_About;
import com.twodstar.photobattle.Fragment_Battles;
import com.twodstar.photobattle.Fragment_Best;
import com.twodstar.photobattle.Fragment_NewBattle;
import com.twodstar.photobattle.MainActivity;
import com.twodstar.photobattle.R;



public class SlideMenuFragment extends SherlockFragment 
{
	private ListView m_ListView1;
	private ListView m_ListView2;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
	{
		
		View view = inflater.inflate(R.layout.sidemenu_listview, null);
		
		m_ListView1 = (ListView) view.findViewById(R.id.list1);
	    m_ListView2 = (ListView) view.findViewById(R.id.list2);
	    
	    String[] Arr1 =getResources().getStringArray(R.array.color_names1);
 		List<String> colors1 =  Arrays.asList(Arr1);		
		ArrayAdapter<String> colorAdapter1 = new SideMenuListAdapter(getActivity(), R.id.text1 , colors1, 1);
		
		
		String[] Arr2 =getResources().getStringArray(R.array.color_names2);
 		List<String> colors2 =  Arrays.asList(Arr2);
		ArrayAdapter<String> colorAdapter2 = new SideMenuListAdapter(getActivity(), R.id.text1 , colors2, 2);
		
		
		m_ListView1.setAdapter(colorAdapter1);
		m_ListView2.setAdapter(colorAdapter2);
		
		m_ListView1.setOnItemClickListener(new AdapterView.OnItemClickListener() 
											{

												@Override
												public void onItemClick(
														AdapterView<?> adapterView,
														View V, 
														int Pos,
														long ID) 
												{
													// TODO Auto-generated method stub
													onListItemClick1(adapterView, V, Pos, ID);
												}
												
											});
		
		m_ListView2.setOnItemClickListener(new AdapterView.OnItemClickListener() 
		{

			@Override
			public void onItemClick(
					AdapterView<?> adapterView,
					View V, 
					int Pos,
					long ID) 
			{
				// TODO Auto-generated method stub
				onListItemClick2(adapterView, V, Pos, ID);
			}
			
		});
		
	    
	    return view;
	}

	

	
	public void onListItemClick1(AdapterView<?> adapterView,
								View V, 
								int position,
								long ID) 
	{
		Fragment newContent = null;
		switch (position) 
		{
		
		// new battle
		case 0:
			switchFragment(MainActivity.SELECTION);
			break;
			
	    // battle list
		case 1:
			switchFragment(MainActivity.BATTLES);
			break;
			
		// best palyer
		case 2:
			switchFragment(MainActivity.BEST);
			break;
	
		}
		
	}
	
	public void onListItemClick2(AdapterView<?> adapterView,
			View V, 
			int position,
			long ID) 
	{
		SherlockFragment newContent = null;
		
		switch (position) 
		{
		
		// Log out
		case 0:			
			OnButtonLogout();
			break;
			
		// About	
		case 1:
			switchFragment(MainActivity.ABOUT);
			break;
		}
		
		
	}


	// the meat of switching the above fragment
	private void switchFragment(int ID) 
	{
		if (getActivity() == null)
			return;
		
		MainActivity fca = (MainActivity)getActivity();
		fca.showFragment(ID);
		 
	}
	
	
	private class SideMenuListAdapter extends ArrayAdapter<String> 
	{        

		private int m_CustomID;
		
        public SideMenuListAdapter(Context context, 
        						   int resourceId, 
        						   List<String> listElements,
        						   int CustomID
        						   ) 
        {
            super(context, resourceId, listElements);
            m_CustomID = CustomID;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            View view = convertView;
            if (view == null) 
            {
                LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.slidmenu_listitem, null);
            }

            String[] ArrStr;
            TypedArray  ArrIcon;
            if(m_CustomID == 1)
            {
            	ArrStr =getResources().getStringArray(R.array.color_names1 );
            	ArrIcon =getResources().obtainTypedArray(R.array.sidemenu_icons1);
            }
            else
            {
            	ArrStr =getResources().getStringArray(R.array.color_names2 );
            	ArrIcon =getResources().obtainTypedArray(R.array.sidemenu_icons2);
            }
            	
            
            ImageView Img_Icon = (ImageView) view.findViewById(R.id.icon);
            Img_Icon.setImageDrawable( ArrIcon.getDrawable(position));
            
            
            TextView  TextView1 = (TextView) view.findViewById(R.id.text1);            
            TextView1.setText( ArrStr[position] );
            
            
               
         
            return view;
        }

    }
	
	
	private void OnButtonLogout()
	{
		if(getActivity() == null)
			return;
		
		DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
		    @Override
		    public void onClick(DialogInterface dialog, int which) 
		    {
		        switch (which)
		        {
		        case DialogInterface.BUTTON_POSITIVE:
		            //Yes button clicked
		        	OnButtonLogoutYes();
		            break;

		        case DialogInterface.BUTTON_NEGATIVE:
		            //No button clicked
		            break;
		        }
		    }
		};

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle("Log Out");
		builder.setIcon(getResources().getDrawable( R.drawable.ic_dialog_info ));
		
		builder.setMessage("Are you sure you want to log out?").setPositiveButton("Confirm", dialogClickListener)
		    .setNegativeButton("Cancel", dialogClickListener).show();
		
		
	}

	
	private void OnButtonLogoutYes()
	{
		if(ParseFacebookUtils.getSession() != null)		
		{
			ParseFacebookUtils.getSession().closeAndClearTokenInformation();
		}
		
		ParseUser.logOut();
		
		if(getActivity() != null)
		{
			((MainActivity)getActivity()).showFragment(MainActivity.SPLASH);
		}
	}


}
